package org.kassa.model;

import org.kassa.dao.ItemDao;
import org.kassa.dao.ItemDaoImpl;

public final class Item {
	private int id;
	private int code;
	private String name;
	private int quantity;
        private double price;
        private ItemDao dao = new ItemDaoImpl();
        
        
        //Constructors
	public Item() {
            this.quantity = 1;
	}
        	
        public Item(int code) {
            setCode(code);
            if (exists()) {
                setPrice(dao.getItemPriceByCode(code));
                setId(dao.getItemIdByCode(code));
                setName(dao.getItemNameById(getId()));
            }
        }
        
        
        //Checkers
        public boolean exists() {
            if (dao.isThereSomeCode(code)) return true;
            return false;
        }
        
        
        //Modifiers
        public boolean sell() {
            if (! exists()) return false;
            Selling selling = new Selling(this);
            return selling.sell();
        }
        
        public boolean register() {
            if (exists()) return false;
            return dao.addItemToList(this);
        }
        
        public boolean updatePrice(double price) {
            if (! exists()) return false;
            setPrice(price);
            return dao.updateItemPrice(this);
        }
        
        public boolean arrive() {
            if (! exists()) return false;
            Arrival arrival = new Arrival(this);
            return arrival.accept();
        }
        
        
        //Getters and setters
	public int getId() {
            if (id == 0) {
                setId(dao.getItemIdByCode(code));
            }
            return id;
	}
	
	public void setId(int id) {
            this.id = id;
	}
	
	public int getCode() {
            return code;
	}
	
	public void setCode(int code) {
            this.code = code;
	}
	
	public String getName() {
            return name;
        }
	
	public void setName(String name) {
            this.name = name;
	}

	public int getQuantity() {
            return quantity;
	}

	public void setQuantity(int quantity) {
            this.quantity = quantity;
	}
        
        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

}
